package com.example.demo.AliPay.Controller;

import com.alipay.api.AlipayApiException;
import com.example.demo.AliPay.Bean.IntegralParameter;
import com.example.demo.AliPay.Bean.Order;
import com.example.demo.AliPay.Service.AliPayService;
import com.example.demo.AliPay.Service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: csp1999
 * @Date: 2020/11/13/21:47
 * @Description: 支付宝沙箱测试 controller
 */
@RestController
public class PayController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private AliPayService aliPayService;
    /*
     * 支付宝支付 api
     */
    @PostMapping(value = "/order/alipay")
    public String alipay(String username,String shopname,Integer product_id,String category,String title,Integer product_nums,String outtradeno,Double price,String status,Integer integral) throws AlipayApiException {
        Order order = new Order();
        order.setId(product_id);
        order.setCategory(category);
        order.setSubject(title);
        order.setProduct_nums(product_nums);
        order.setOut_trade_no(outtradeno);
        order.setTotal_amount(price);
        order.setStatus(status);
        order.setUsername(username);
        order.setShopname(shopname);
        orderService.insertOrders(order);
        IntegralParameter integralParameter = new IntegralParameter();
        integralParameter.setUsername(username);
        integralParameter.setIntegral(integral);
        orderService.updateintegral(integralParameter);
        return aliPayService.aliPay(order);
    }

    @PostMapping("/paySuccess")
    public Boolean paySuccess(String out_trade_no){
        try{
            orderService.updateOrders(out_trade_no);
            return true;
        }catch (Exception e){
            return false;
        }
    }

}